﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using Edu.Web.Biz;
using Edu.Web.Data.Service;
using Edu.Web.Security;
using ESoft.Core.Logging;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    [AllowAnonymous]
    public class AccountController : BaseController
    {

        private readonly ILogger logger = LogManager.GetLogger(typeof(AccountController));
        private readonly IAccountService _accountService;

        public AccountController(IAccountService accountService)
        {
            _accountService = accountService;
        }

        // GET: Account
        public ActionResult Login()
        {
            return View("Login");
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UserLogin()
        {
            try
            {
                string userName = GetRequestMap("userName", "");
                string password = GetRequestMap("userPassword", "");

                if (userName.IsNullOrWhiteSpace())
                {
                    throw new Exception("用户名不能为空！");
                }
              
                if (password.IsNullOrWhiteSpace())
                {
                    throw new Exception("密码不能为空！");
                }
                User user = _accountService.SignIn(userName, password);

                FormsAuthentication<User>.SetAuthCookie(user.UserName, user, true);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return Redirect("/Account/Login");
        }
    }
}